<template>
  <div class="head_wraper">
    <div class="head_top_wrap">
      <div class="head_top">
        <h1>
          欢迎来到睿那体育在线商城
        </h1>
        <div class="login_wrap" v-if="!hasInfo">
          <span class="login"><span @click="toLogin('login')">登录</span></span>
          <span class="line"></span>
          <span class="register"><span @click="toLogin('register')">注册</span></span>
        </div>
        <div class="user_info_wrap" v-else>
          <div class="user_ioc_wrap" @click="gotoInfo">
            <span class="user_head_img">
              <img :src="userInfo.userImg" alt="">
            </span>
            <span class="user_info_name">{{userInfo.userName}}</span>
          </div>
          <div @click="changeLogin" style="color:#999;">【切换账号】</div>  
          <el-popover
            ref="popover1"
            placement="bottom"
            width="100"
            trigger="hover">
              <router-link tag="p" to="/car" style="text-align:center;line-height:24px;cursor: pointer;">购物车</router-link>
              <router-link tag="p" to="/listAction" style="text-align:center;line-height:24px;cursor: pointer;">订单</router-link>
          </el-popover>
          <div v-popover:popover1>更多</div>
        </div>
      </div>
    </div>
    <div class="nav_wrap">
      <h1 class="logo">
        <a href="/">睿那体育</a>
      </h1>
      <nav class="nav">
        <router-link class="home" tag="span" to="/">首页</router-link>
        <router-link class="choose" tag="span" to="/classlist">选课中心</router-link>
        <router-link class="study" tag="span" to="/study">学习中心</router-link>
        <router-link class="shop" tag="span" to="/shoplist">体育商城</router-link>
        <router-link class="match" tag="span" to="/match">赛事报名</router-link>
        <span @click="freeStudy">免费试课</span>
      </nav>
      <el-dialog
        title="请选择参加人员"
        :visible.sync="dialogVisible"
        width="50%"
        :before-close="handleClose">
        <div class="user_item" @click="getChoiceID(item.id,index)" :class="{active:activeIndex==index}" v-for="(item,index) in studyInfo" :key="item.key">
          <p>{{item.name}}</p>
          <p>性别：{{item.sex}}</p>
          <p>出生日期：{{item.birth}}</p>
          <p>身份证号：{{GLOBAL.hideCard(item.cardId)}}</p>
        </div>
        <div v-if="status&&!errMsg" style="text-align:center;">获取信息中，请等待...</div>
        <div v-if="errMsg" style="text-align:center;">{{errMsg}}</div>
        <div v-if="!status&&!studyInfo.length">没有获取到您的信息,可以点击
          <span class="goto_add" @click="goToAdd">这里</span>添加信息
        </div>
        <span slot="footer" class="dialog-footer">
          <el-button @click="goToAdd">添加信息</el-button>
          <el-button :loading='isLoading' type="primary" @click="ok">确 定</el-button>
        </span>
      </el-dialog>
      <div class="search" v-if="path=='classlist'||path=='shoplist'">
        <input class="searchInput" type="text" @keyup.enter="search" v-model="searchVal" :placeholder="path=='classlist'?'课程名称关键字':'商品名称关键字'">
        <span class="el-icon-search search_ioc" @click="search"></span>
      </div>
    </div>
  </div>
</template>
<script>
export default {
   name:'headTop',
   components:{
     
   },
   data() {
      return {
        activeIndex: 0,
        searchVal:'',
        userInfo:{
          userImg:'',
          userName:''
        },
        studyInfo:[],
        hasInfo:false,
        dialogVisible:false,
        choiceId:'',
        status:false,
        path:this.$route.path.slice(1),
        errMsg:'',
        isLoading:false
      };
    },
    beforeMount(){

    },
    mounted(){
      //用户信息
      let username = this.$cookie.get("rnty_username");
      let userimg = this.$cookie.get("rnty_userimg");
      let token = this.$cookie.get("rnty_access_token")
      if(token){//token存在，登陆过
        if(username&&userimg){//需展示内容存在
          this.userInfo.userImg = userimg;
          this.userInfo.userName = username
        }else{//需展示内容缺少
          this.GLOBAL.tokenRequest({
            baseURL:this.GLOBAL.API_ACCOUNT_RNTY_URL,
            url:'users/info'
          }).then(({data:data})=>{
            this.$cookie.set("rnty_username", data.username, { domain: '.ruinatiyu.com'});
            this.$cookie.set("rnty_userimg",data.headImg,{ domain: '.ruinatiyu.com'});
            this.userInfo.userImg = data.headImg;
            this.userInfo.userName = data.username
            //console.log(this.userInfo);
          })
        }
        this.hasInfo = true;
      }else{//没登录过
        this.hasInfo = false;
        return;
      }
    },
    methods: {
      search(){
        if (this.searchVal.trim()=='') {
          this.$router.push({path:this.$route.path})
          return
        }
        this.$router.push({path:this.$route.path,query:{search:this.searchVal}})
      },
      changeLogin(){
        this.GLOBAL.authLogin();
      },
      getChoiceID(id,index){
        this.activeIndex = index;
        this.choiceId = id;
      },
      ok(){
        this.isLoading = true;
        this.GLOBAL.tokenRequest({
          method:'post',
          baseURL:this.GLOBAL.RNTY_SHOP_MALL_URL,
          url:'trys/joins',
          data:{
            id:this.choiceId
          }
        }).then(({data:data})=>{
          this.isLoading = false;
          this.dialogVisible = false;
          if (data) {
            this.$message({
              message:'报名成功',
              type:'success'
            })
          }else{
            this.$message({
              message:'不可重复报名',
              type:'error'
            })
          }
        })
      },
      goToAdd(){
        this.GLOBAL.authLogin('user')
      },
      freeStudy(){
        this.status = true;
        this.GLOBAL.tokenRequest({
          method:"post",
          baseURL:this.GLOBAL.RNTY_SHOP_MALL_URL,
          url:'students/show',
        }).then(({data:data})=>{
          //console.log(data)
          this.status = false;
          this.studyInfo = data;
          this.choiceId = data[0]?data[0].id:'';
        }).catch((err)=>{
          this.errMsg = '获取信息失败'
        })
        this.dialogVisible = true
      },
      handleClose(done) {
        this.$confirm('确认取消吗？')
          .then(_ => {
            done();
          })
          .catch(_ => {});
      },
      toLogin(methods){
        this.GLOBAL.authLogin(methods)
      },
      gotoInfo(){
        this.GLOBAL.authLogin('user')
      },
    }
}
</script>
<style scoped>
/* 吊顶区域 */
.head_wraper{
  height:140px;
  box-shadow: 0px 3px 10px rgba(0,0,0,.3);
}
.head_top_wrap{
  background-color: #f9f9f9;
}
.head_top{
  height:50px;
  width:1200px;
  padding-top: 8px;
  margin:0 auto;
  color: #999;
  font:14px/42px "微软雅黑";
  display: flex;
  justify-content: space-between;
}
.head_top h1{
  font:14px/42px "微软雅黑";
}
.login_wrap{
  width:85px;
  display: flex;
  justify-content: space-between;
  align-items: center;
}
.login_wrap .login, .login_wrap .register{
  cursor: pointer;
}
.login_wrap .line{
  width:1px;
  height:15px;
  background-color: #999;
}
.login_wrap .login{
  color:#0078c5;
}
.user_head_img{
  width:40px;
  height:40px;
  border-radius: 50%;
  overflow: hidden;
  cursor: pointer;
}
.user_head_img img{
  width:40px;
  height:40px;
}
.user_info_name{
  width:58px;
  white-space: nowrap;
  text-overflow: ellipsis;
  overflow: hidden;
}
.user_info_wrap{
  cursor: pointer;
  width:250px;
  display: flex;
  justify-content: space-around;
  align-items: center;
}
.user_ioc_wrap{
  display: flex;
  width:98px;
  justify-content: space-between;
}
/* 导航区域 */
.nav_wrap{
  height:82px;
  width: 1200px;
  margin: 0 auto;
  display: flex;
}
.logo{
  width:140px;
  height:82px;
  overflow: hidden;
}
.logo a{
  display: inline-block;
  width:140px;
  height:82px;
  text-indent: -999px;
  background:url('../assets/images/logo.png') no-repeat 0 5px;
  background-size:140px 70px;
}
.nav{
  margin-left:8px;
  width:787px;
}
.nav span{
  cursor: pointer;
  font: 18px/82px '微软雅黑';
  color:#666;
  padding:0 30px;
}
.search{
  width:264px;
}
.search input{
  padding-top:20px;
  height:42px;
  font: 16px/42px "微软雅黑";
  color:#999;
  border: none;
  border-bottom: 1px solid #057ec7;
}
.search_ioc{
  cursor: pointer;
}
.user_item{
  padding:5px;
  box-sizing: border-box;
  width:230px;
  height:105px;
  border:1px solid #73caff;
  position: relative;
  display: inline-block;
  margin-right:15px;
  margin-bottom: 15px;
  cursor: pointer;
}
.active::after{
  position: absolute;
  content: '√';
  width:25px;
  height:25px;
  bottom: 0;
  right:0;
  text-align: center;
  font-family: Arial, Helvetica, sans-serif;
  color:#fff;
  background-color: #73caff;
}
.goto_add{
  padding:0 10px;
  font-size:20px;
  color:#73caff;
  cursor: pointer;
  text-decoration: underline;
}
.searchInput::-webkit-input-placeholder{
  color: #e5e5e5;
  font:14px/16px "微软雅黑";
}
.searchInput::-moz-input-placeholder{
  color: #e5e5e5;
  font:14px/16px "微软雅黑";
}
.searchInput::-ms-input-placeholder{
  color: #e5e5e5;
  font:14px/16px "微软雅黑";
}
</style>
